﻿using Abp.AutoMapper;
using Abp.Domain.Uow;
using Abp.EntityFrameworkCore;
using Abp.Modules;
using Abp.Reflection.Extensions;
using AVISOutBound.SSOServer.EntityFrameworkCore.Models;
using AVISOutBound.SSOServer.Orders;

namespace AVISOutBound.SSOServer.EntityFrameworkCore
{
    [DependsOn(
        typeof(SSOServerCoreModule),
        typeof(AbpEntityFrameworkCoreModule))]
    public class SSOServerEntityFrameworkCoreModule : AbpModule
    {
        public override void PreInitialize()
        {
            // lulus:全局禁用租户相关的filter
            Configuration.UnitOfWork.OverrideFilter(AbpDataFilters.MustHaveTenant, false);
            Configuration.UnitOfWork.OverrideFilter(AbpDataFilters.MayHaveTenant, false);

            // 配置对象映射的关系
            Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
            {
                //查询订单
                config.CreateMap<avisOBOrder, Order>()
                      //.ForMember(u => u.CarGroup, options => options.Ignore()) // 忽略
                      .ForMember(u => u.SIPPCode, options => options.MapFrom(input => input.CarGroup))
                      .ForMember(u => u.Amount, options => options.MapFrom(input => input.QuotedTMI.Value))//订单金额
                      .ForMember(u => u.BookingDate, options => options.MapFrom(input => input.CreateDate))//下单时间
                      .ForMember(u => u.ReturnDateTime, options => options.MapFrom(input => input.ReturnDate))//还车时间
                      .ForMember(u => u.ReturnStoreCode, options => options.MapFrom(input => input.ReturnLoc))//还车门店编号
                      .ForMember(u => u.PickupDateTime, options => options.MapFrom(input => input.PickupDate))//取车时间
                      .ForMember(u => u.PickUpStoreCode, options => options.MapFrom(input => input.PickupLoc))//取车门店编号
                      .ForMember(u => u.RateCode, options => options.MapFrom(input => input.LastRateCode))//套餐价格代码
                      .ForMember(u => u.RMBAmount, options => options.MapFrom(input => input.AmountCNY.HasValue ? input.AmountCNY.Value : 0))//人民币价格
                      .ForMember(u => u.OrderNo, options => options.MapFrom(input => input.ReservationNumber));//订单号
                //保存订单
                config.CreateMap<Order, avisOBOrder>()
                      .ForMember(u => u.Id, options => options.Ignore())
                      .ForMember(u => u.CarGroup, options => options.MapFrom(input => input.SIPPCode))//预定车组唯一码
                      .ForMember(u => u.QuotedTMI, options => options.MapFrom(input => input.Amount))//订单金额
                      .ForMember(u => u.ReturnDate, options => options.MapFrom(input => input.ReturnDateTime))//还车时间
                      .ForMember(u => u.ReturnLoc, options => options.MapFrom(input => input.ReturnStoreCode))//还车门店编号
                      .ForMember(u => u.PickupDate, options => options.MapFrom(input => input.PickupDateTime))//取车时间
                      .ForMember(u => u.PickupLoc, options => options.MapFrom(input => input.PickUpStoreCode))//取车门店编号
                      .ForMember(u => u.LastRateCode, options => options.MapFrom(input => input.RateCode))//套餐价格代码
                      .ForMember(u => u.AmountCNY, options => options.MapFrom(input => input.RMBAmount))//人民币价格
                      .ForMember(u => u.ReservationNumber, options => options.MapFrom(input => input.OrderNo));//订单号

            });
            base.PreInitialize();
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(SSOServerEntityFrameworkCoreModule).GetAssembly());
        }
    }
}